include('shared.lua')

function PillboxMenu(um)
	if PillBoxFrame then PillBoxFrame:Remove() end
	
	local drugs = glon.decode(um:ReadString())
	local pillboxid = um:ReadShort()
	
	local localdruglist = GetDrugList()
	
	PillBoxFrame = vgui.Create("DFrame")
	PillBoxFrame:SetSize(230, 415)
	PillBoxFrame:Center()
	
	if #drugs == 0 then
		PillBoxFrame:SetTitle("Pillbox (Empty)")
	else
		PillBoxFrame:SetTitle("Pillbox (" .. table.Count(drugs).. " items)")
	end
	
	PillBoxFrame:MakePopup()
	
	local DrugList = vgui.Create("DPanelList")
		DrugList:EnableVerticalScrollbar(true)
		DrugList:SetParent(PillBoxFrame)
		DrugList:SetPos(5, 27)
		DrugList:SetSize(PillBoxFrame:GetWide() - 10, PillBoxFrame:GetTall() - 33)
		DrugList:SetPadding(5)
		DrugList:SetSpacing(5)
	
	for k, v in pairs(drugs) do
		local Panel = vgui.Create("DPanel")
		
		local Label = vgui.Create("DLabel")
			Label:SetParent(Panel)
			Label:SetPos(5, 2)
			Label:SetText(scripted_ents.Get(localdruglist[v]).PrintName)
			Label:SetFont("GmodNotify")
			Label:SetTextColor(Color(255, 255, 255))
		
		local Button = vgui.Create("DButton")
			Button:SetParent(Panel)
			Button:SetSize(69, 20)
			Button:SetPos(DrugList:GetWide() - 100, 2)
			Button:SetText("Withdraw")
			function Button.DoClick()
				RunConsoleCommand("pillboxwithdraw", pillboxid, k, v)
			end
		
		DrugList:AddItem(Panel)
	end
end
usermessage.Hook("pillboxmenu", PillboxMenu)
